package com.hnichr.ump.talent.library.service.impl;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.common.schedule.dto.RunResultDTO;
import com.hnichr.ump.common.talent.library.entity.EducationInfo;
import com.hnichr.ump.common.talent.library.entity.PersonalInfo;
import com.hnichr.ump.common.talent.library.entity.SchoolList;
import com.hnichr.ump.common.talent.library.handle.SchoolTypeHandle;
import com.hnichr.ump.common.talent.library.service.*;
import com.hnichr.ump.talent.library.provider.SyncPolicyProvider;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hnichr.ump.common.talent.library.entity.Resume;

import java.util.List;

@Service
public class SyncServiceImpl implements SyncService {
    private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALGBILhOuNuVmDfyfA9LwE0mxz15c+eUxsTEYGa3C3IE8WAj03t3RY+TsmFQ3Z/3zSn5q9pVnYz5S+00Ahr8stOZ++wVQlno0TZJF/RgoGl8nzwC/n9NM8dskka23GyZ5mXkmiu6sZYvL1H8LpB2UOd3SenFJJ6X9wnDchtQ8pOlAgMBAAECgYALQJbM6DyiuuR343jG1XP+0A8AUCMvUoiZL6O2zzK8CyEsCYleAc0YkXHU5nXuLpzEZO3mfNfseF8+TbY2dL93PHxZe3Ae4fumb6KaB7qAkuTDb3KGI/5D0sxtoop/mOg6FRGnj5/hvoriv9aNGp6tK5dCYSUIBNyqVTdiiKoTjwJBANV0Vultg/PDoPgT7KVKIn8Yq6TQMintNo5HRtY37rFhe0xN/20CyUVWhq/2xhj7zdiTyCardSmtB594QlcXqhMCQQDU4mmWlOKlAbn2doKFHAcgsJlpvLMWP7Thmtzw3wWxkKfXoNSxiptYSuVxDFI52lXWRCLjXq+dYtwovPOfowJnAkAwHM0KSHkZlNrPkA4jI5aq9zCZMaRc9u75Gu/IkUroCIT2Kg6Mz7f2PQ6W0xTLxNYdHxJaRQNfpXS2aTDq2JapAkEAtuLj7xdtNnCrQA9DxfL0NKNCyNHx0JVeYcrXMJ+Ilqk9aEOlNflrx4hgYKRDJ6xovbC/gDHiEQXTObRKX8R81QJBAKktZ1y3zlz5G8kGGJa4LVX9a/M04++VtGGIQ8Cf0o8hIV4sQc0bYL69lidYUM4igJF9C7zd81nfI4UfCfGIC24=";
    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxgSC4TrjblZg38nwPS8BNJsc9eXPnlMbExGBmtwtyBPFgI9N7d0WPk7JhUN2f980p+avaVZ2M+UvtNAIa/LLTmfvsFUJZ6NE2SRf0YKBpfJ88Av5/TTPHbJJGttxsmeZl5JorurGWLy9R/C6QdlDnd0npxSSel/cJw3IbUPKTpQIDAQAB";

    private static final String SYNC_URL = "/admin/api/sync/resume/";

    private static final Logger logger = LoggerFactory.getLogger(SyncServiceImpl.class);

    @Value("${huanghehr.url}")
    private String url;

    @Autowired
    private List<SyncPolicyProvider> syncPolicyList;

    @Autowired
    private SchoolListService schoolListService;

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private EducationInfoService educationInfoService;

    @Autowired
    private PersonalInfoService personalInfoService;

    /**
     * 同步黄河人才网简历
     */
    @Override
    public RunResultDTO syncHuanghehrResume(String syncDate){
        String result = null;
        try{
            logger.info(">>>>>>>>>>>>>>>>>开始同步：{}",url+SYNC_URL+syncDate);
            result = HttpUtil.get(url+SYNC_URL+syncDate);
        }
        catch (Exception e){
            logger.error("数据访问接口报错",e);
            return RunResultDTO.failed("数据访问接口报错");
        }
        if(StringUtils.isBlank(result)){
            logger.error("数据访问接口未返回数据");
            return RunResultDTO.failed("数据访问接口未返回数据");
        }
        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        Boolean success = resultObject.getBoolean("success");

        if(!(code!=null&&success!=null&&code.equals(200)&&success.equals(true))){
            logger.error("数据访问接口调用失败,code:{},success:{}",code,success);
            return RunResultDTO.failed("数据访问接口调用失败");
        }

        String message = resultObject.getString("message");
        RSA deRSA = new RSA(PRIVATE_KEY,null);
        String json = new String(deRSA.decrypt(HexUtil.decodeHex(message), KeyType.PrivateKey), CharsetUtil.CHARSET_UTF_8);
        JSONObject syncData = JSON.parseObject(json);
        for(SyncPolicyProvider provider:syncPolicyList){
            if(provider.support(syncData)){
                provider.sync(syncDate, syncData);
            }
        }

        for(SyncPolicyProvider provider:syncPolicyList){
            provider.postSync(syncDate);
        }

        return RunResultDTO.success();
    }
}
